package com.codeshellme.bookmanager.controller;

import com.codeshellme.bookmanager.dto.BookStock;
import com.codeshellme.bookmanager.service.BookStockService;
import com.codeshellme.bookmanager.utils.CommonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class BookStockController {

    @Autowired
    private BookStockService bookStockService;

    @RequestMapping("/book_stock")
    public String book_stock() {
        return "book_stock";
    }

    // 分页查询库存信息
    @ResponseBody
    @RequestMapping("/queryAllBookStocks")
    public CommonResult queryAllBookStocks(String name, Integer type_id, Integer num_start, Integer num_end,
                                           Integer author_id, Byte status, String date_start, String date_end,
                                           String q,
                                           Integer page, Integer rows) {

        PageInfo<Map<String, Object>> pageInfo = bookStockService.queryAllBookStocks(
                name, type_id, num_start, num_end, author_id, status, date_start, date_end, q, page, rows);

        // 构建 EasyUI 分页需要的数据
        Map<String, Object> ret = new HashMap<>();
        ret.put("rows", pageInfo.getList());
        ret.put("total", pageInfo.getTotal());

        return CommonResult.success(ret);
    }

    @ResponseBody
    @RequestMapping("/addBookStock")
    public CommonResult addBookStock(BookStock stock) {
        int ret = bookStockService.addBookStock(stock);
        if (ret < 0) {
            return CommonResult.failed("新增库存失败");
        } else {
            return CommonResult.success("新增库存成功");
        }
    }

    @ResponseBody
    @RequestMapping("/verifyBookStockId")
    public Boolean verifyBookStockId(String bookName) {
        return bookStockService.verifyBookStockId(bookName);
    }

    // 更新库存数量
    @ResponseBody
    @RequestMapping("/updateBookStockNum")
    public CommonResult updateBookStockNum(Integer id, Integer num, String op) {

        try {
            bookStockService.updateBookStockNum(id, num, op);
            return CommonResult.success("库存操作成功");
        } catch (DataIntegrityViolationException e) {
            return CommonResult.failed("库存操作失败：您操作的库存量超出范围");
        } catch (Exception e) {
            return CommonResult.failed("库存操作失败：未知错误");
        }
    }

    @ResponseBody
    @RequestMapping("/deleteBookStockById")
    public CommonResult deleteBookStockById(@RequestParam(value = "id") Integer stockId) {
        int ret = bookStockService.deleteBookStockById(stockId);
        if (ret != 1) {
            return CommonResult.failed("删除库存失败：还有库存不能删除");
        } else {
            return CommonResult.success("删除库存成功");
        }
    }
}
